package org.dromara.llm.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.llm.domain.vo.SysMessageVo;
import org.dromara.llm.domain.bo.SysMessageBo;
import org.dromara.llm.service.ISysMessageService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 人与AI对话消息
 *
 * @author Miles Meng
 * @date 2025-06-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/llm/message")
public class SysMessageController extends BaseController {

    private final ISysMessageService sysMessageService;

    /**
     * 查询人与AI对话消息列表
     */
    @SaCheckPermission("system:message:list")
    @GetMapping("/list")
    public TableDataInfo<SysMessageVo> list(SysMessageBo bo, PageQuery pageQuery) {
        return sysMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出人与AI对话消息列表
     */
    @SaCheckPermission("system:message:export")
    @Log(title = "人与AI对话消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysMessageBo bo, HttpServletResponse response) {
        List<SysMessageVo> list = sysMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "人与AI对话消息", SysMessageVo.class, response);
    }

    /**
     * 获取人与AI对话消息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:message:query")
    @GetMapping("/{id}")
    public R<SysMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysMessageService.queryById(id));
    }

    /**
     * 获取人与AI对话消息详细信息
     *
     * @param bo sessionId
     */
    @SaIgnore
    @GetMapping("/getMessageInfoBySessionId")
    public R<SysMessageVo> getMessageInfo(SysMessageBo bo) {
        return R.ok(sysMessageService.queryBysessionId(bo));
    }

    /**
     * 新增人与AI对话消息
     */
    @SaCheckPermission("system:message:add")
    @Log(title = "人与AI对话消息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysMessageBo bo) {
        return toAjax(sysMessageService.insertByBo(bo));
    }

    /**
     * 修改人与AI对话消息
     */
    @SaCheckPermission("system:message:edit")
    @Log(title = "人与AI对话消息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysMessageBo bo) {
        return toAjax(sysMessageService.updateByBo(bo));
    }

    /**
     * 删除人与AI对话消息
     *
     * @param messageIds 主键串
     */
    @SaCheckPermission("system:message:remove")
    @Log(title = "人与AI对话消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{messageIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] messageIds) {
        return toAjax(sysMessageService.deleteWithValidByIds(List.of(messageIds), true));
    }
}
